package com.cheng.example.leetcode.mult.T1114.v3;

import java.util.concurrent.Semaphore;

public class Foo {
    //声明两个 Semaphore变量
    private Semaphore spa, spb;

    public Foo() {
        //初始化Semaphore为0的原因：如果这个Semaphore为零，如果另一线程调用(acquire)这个Semaphore就会产生阻塞，便可以控制second和third线程的执行
        spa = new Semaphore(0);
        spb = new Semaphore(0);
    }

    public void first(Runnable printFirst) throws InterruptedException {
        // printFirst.run() outputs "first". Do not change or remove this line.
        printFirst.run();
        //只有等first线程释放Semaphore后使Semaphore值为1,另外一个线程才可以调用（acquire）
        spa.release();
    }

    public void second(Runnable printSecond) throws InterruptedException {
        //只有spa为1才能执行acquire，如果为0就会产生阻塞
        spa.acquire();
        // printSecond.run() outputs "second". Do not change or remove this line.
        printSecond.run();
        spb.release();
    }

    public void third(Runnable printThird) throws InterruptedException {
        //只有spb为1才能通过，如果为0就会阻塞
        spb.acquire();
        // printThird.run() outputs "third". Do not change or remove this line.
        printThird.run();
    }


    public static void main(String[] args) {
       Foo foo = new Foo();

        new Thread(() -> {
            try {
                for (int j = 0; j < 1; j++) {
                    foo.first(() -> {
                        System.out.println("first");
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                for (int j = 0; j < 1; j++) {
                    foo.third(() -> {
                        System.out.println("third");
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();


        new Thread(() -> {
            try {
                for (int j = 0; j < 1; j++) {
                    foo.second(() -> {
                        System.out.println("second");
                    });
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

    }
}
